package com.woniu.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.job.entity.Admin;
import com.woniu.job.exception.service.AdminAccountAndPasswordNotMatch;
import com.woniu.job.exception.service.AdminExistException;
import com.woniu.job.exception.service.ForbiddenAdminException;
import com.woniu.job.exception.service.ModifySuperAdminException;
import com.woniu.job.mapper.AdminMapper;
import com.woniu.job.service.IAdminService;
import com.woniu.job.util.Helper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 *  状态：1正常   2 已禁用  3已删除
 * </p>
 *
 * @author song
 * @since 2019-12-24
 */
@Service
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    AdminMapper adminMapper;

    /**
     * 根据账户名密码获取
     *  账户禁用，抛异常
     * @param account
     * @param password
     * @return
     */
    @Override
    public Admin getByNameAndPassword(String account, String password)
    {
        QueryWrapper query = new QueryWrapper();
        query.eq("account", account);
        Admin admin = adminMapper.selectOne(query);
        // 比较密码
        if (admin != null && admin.getPassword().equals(Helper.getMD5(password))) {
            if(admin.getStatus().equals(IAdminService.STATUS_FORBIDDEN))
            {
                throw new ForbiddenAdminException("禁用账户，联系管理员");
            }
            return admin;
        }
        throw new AdminAccountAndPasswordNotMatch("用户名密码不匹配");
    }

    /**
     * 添加管理员
     * @param admin
     */
    @Override
    public void add(Admin admin)
    {
        // 是否存在
        QueryWrapper<Admin> query = new QueryWrapper<>();
        query.eq("account", admin.getAccount());
        if (adminMapper.selectOne(query) != null) {
            throw new AdminExistException();
        }
        // 获取id
        admin.id(Helper.getId());
        // 加密密码
        admin.password(Helper.getMD5(admin.getPassword()));
        adminMapper.insert(admin);
    }

    /**
     * 获取列表,分页
     *  page size 3
     * @return 管理员列表
     */
    @Override
    public IPage<Admin> getList(int p)
    {
        Page<Admin> page = new Page<>(p, 3);
        IPage<Admin> iPage = adminMapper.selectPage(page, null);
        return iPage;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id)
    {
        if (adminMapper.selectById(id).getAccount().equals(IAdminService.SuperAdmin)) {
            throw new ModifySuperAdminException("无法删除系统超级管理员");
        }
        adminMapper.deleteById(id);
    }

    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public void updateStatus(String id, String status)
    {
        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("account", IAdminService.SuperAdmin));
        if(id.equals(admin.getId()))
        {
            throw new ModifySuperAdminException();
        }
        adminMapper.updateById(new Admin().id(id).status(status));

    }
}
